package com.fht.spring_boot_study.model.test.controller;

import com.fht.spring_boot_study.model.common.entity.ResultEntity;
import com.fht.spring_boot_study.model.common.entity.SearchBean;
import com.fht.spring_boot_study.model.test.entity.Student;
import com.fht.spring_boot_study.model.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;
    /**
     * 127.0.0.1/api/student ---- post
     * {"studentName":"fuhaotian","age":18,"sex":"male","studentCard":{"cardNo":"2021020101"}}
     */
    @PostMapping(value = "/student", consumes = "application/json")
    public ResultEntity<Student> insertStudent(@RequestBody Student student) {
        return studentService.insertStudent(student);
    }

    /**
     * 127.0.0.1/api/student ---- put
     * {"id":3,"studentName":"fuhaotian","age":18,"sex":"male"}
     */
    @PutMapping(value = "/student", consumes = "application/json")
    public ResultEntity<Student> updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    /**
     * 127.0.0.1/api/student/2 ---- delete
     */
    @DeleteMapping("/student/{id}")
    public ResultEntity<Student> deleteStudent(@PathVariable int id) {
        return studentService.deleteStudent(id);
    }

    /**
     * 127.0.0.1/api/student?studentName=fuhaotian&age=18 ---- get
     */
    @GetMapping("/student")
    public Student findByStudentNameAndAge(@RequestParam String studentName,
                                           @RequestParam int age) {
        return studentService.findByStudentNameAndAge(studentName, age);
    }
    /**
     * 127.0.0.1/api/students ---- post
     * {"currentPage":1, "pageSize":5, "orderBy":"studentName",
     * "direction":"desc", "keyword":"fu"}
     */
    @PostMapping(value = "/students", consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean) {
        return studentService.getStudentsBySearchBean(searchBean);
    }

}
